package ccuserinterface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import datamodels.Conference;
import datamodels.GlobalDataStore;
import datamodels.Role;
import datamodels.User;
/**
* 
* A conference creator GUI. The main GUI class will call on the other GUI to perform the user stories and action.
* 
* @author Rithy Chhen, Alan Meaker, Zach Turk and Anthony Sierra
* @version 11.20.2011
*/
@SuppressWarnings("serial")
public class ConferenceCreatorStart extends JFrame
{
    /**
     * This is the variable that hold everything
     * it know how many conferences there are.
     * what those conferences contain
     * the users
     * everything. This variable will be the default variable to be looking after
     * in the program
     * DO NOT REMOVE THIS VARIABLE! @author anthony
     */
    GlobalDataStore my_data_store = new GlobalDataStore();
    /**Combo box drop down containing the current conferences*/
    JComboBox my_conferences = new JComboBox();
    /**Combo box drop down containing the user in the conference. */
    JComboBox my_users = new JComboBox();
    /**Combo box drop down containing the different User Roles*/
    JComboBox my_user_roles = new JComboBox();
    /**Combo box drop down containing the different User Actions*/
    JComboBox my_user_actions = new JComboBox();
    /**Metric for getting the current font sizes for certain text*/
    FontMetrics metrics = this.getFontMetrics(new Font("Comic Sans MS", Font.BOLD, 16));
    JButton create_conference_btn;
    JLabel or_lbl;
    JButton create_user_btn;
    JButton assign_role_btn;
    
    private Conference my_conference; 
    private User my_user;
    public ConferenceCreatorStart()
    {
    	setTitle("Conference Creator");
        setLayout(null);
        setVisible(true);
        setPreferredSize(new Dimension(500,400));
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setupGUI();
        pack();
        JOptionPane.showMessageDialog(this, "Hi there welcome to conference creator!\n On the screen you current have in front of you," +
                " there is two options.\n Choose a previous conference that you have created from earlier,\n or create a new conference.",
                "Welcome to Conference Creator", JOptionPane.INFORMATION_MESSAGE);
    }
    /**
     * This will setup the GUI and the start for choosing your conference.
     */
    private void setupGUI()
    {
        //Create a label saying select the conference
        JLabel selectConferece = new JLabel("Select Conference:");
        add(selectConferece);
        //Create a seperator label for introducing the other conference.
        or_lbl = new JLabel("or");
        or_lbl.setBounds(new Rectangle(270, 30, 50, 16));
        add(or_lbl);
        
        //Create a create conference button
        create_conference_btn = ImageCreator.createButton("   Create a Conference", new Rectangle(300, 15, 145, 40));
        create_conference_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
            	
                String conference_name = JOptionPane.showInputDialog(null, "Please add a conference", "Add a conference");
                if (conference_name.length() < 2)
                {
                    return;
                }
                my_data_store.addConferenceObject(new Conference(conference_name));
                remove(my_conferences);
                my_conferences = new JComboBox();
                //Get the dimensions needed to say select a conference
                int hgt = metrics.getHeight();
                int adv = metrics.stringWidth("Select a conference");
                Dimension size = new Dimension(adv+2, hgt+2);
                my_conferences.setBounds(10,20,100 + size.width,30);
                my_conferences.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        
                        if (my_conferences.getSelectedItem().equals("Select a conference"))
                        {
                            return;
                        }
                        create_conference_btn.setEnabled(false);
                        my_conferences.setEnabled(false);
                        for (int i = 0; i < my_data_store.getConferences().size();i++)
                        {
                            if (my_data_store.getConferences().get(i).getName().equals((String)my_conferences.getSelectedItem()))
                            {
                                my_data_store.setConference(my_data_store.getConferences().get(i));
                            }
                        }
                        for (int i = 0; i < my_data_store.getConferences().size(); i++)
                        {
                            if (my_data_store.getConferences().get(i).getName().equals((String)my_conferences.getSelectedItem())){
                                my_conference = (Conference) my_data_store.getConferences().get(i);
                                break;
                            }
                        }
                        displayUserTypeDropDown();
                    }
                });
                add(my_conferences);
                my_conferences.addItem("Select a conference");
                for (int i = 0; i < my_data_store.getConferences().size(); i++)
                {
                    my_conferences.addItem(my_data_store.getConferences().get(i).getName());
                }
            }
        });
        add(create_conference_btn);
        
        //Get the dimensions needed to say select a conference
        int hgt = metrics.getHeight();
        int adv = metrics.stringWidth("Select a conference");
        Dimension size = new Dimension(adv+2, hgt+2);
        my_conferences.setBounds(10,20,100 + size.width,30);
        selectConferece.setBounds(my_conferences.getBounds().x + 5,my_conferences.getBounds().y - 15, 200, 10);
        
        //Add the possible items onto the list.
        my_conferences.addItem("Select a conference");
        for (int i = 0; i < my_data_store.getConferences().size(); i++)
        {
            my_conferences.addItem(my_data_store.getConferences().get(i).getName());
        }
        my_conferences.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
                if (my_conferences.getSelectedItem().equals("Select a conference"))
                {
                    return;
                }
                create_conference_btn.setEnabled(false);
                my_conferences.setEnabled(false);
                for (int i = 0; i < my_data_store.getConferences().size();i++)
                {
                    if (my_data_store.getConferences().get(i).getName().equals((String)my_conferences.getSelectedItem()))
                    {
                        my_data_store.setConference(my_data_store.getConferences().get(i));
                    }
                }
                for (int i = 0; i < my_data_store.getConferences().size(); i++)
                {
                    if (my_data_store.getConferences().get(i).getName().equals((String)my_conferences.getSelectedItem())){
                        my_conference = (Conference) my_data_store.getConferences().get(i);
                        break;
                    }
                }
                displayUserTypeDropDown();
            }
        });
        add(my_conferences);
    }
    /**
     * This will extract the users from the current conference and display them.
     */
    private void displayUserTypeDropDown()
    {
        JLabel selectRole = new JLabel("Select user");
        add(selectRole);
        //set the bounds of the stuff 
        my_users.setBounds(my_conferences.getBounds().x, my_conferences.getBounds().y + my_conferences.getBounds().height + 50,300,30);
        selectRole.setBounds(my_users.getBounds().x + 5,my_users.getBounds().y - 15, 350, 15);
        my_users.addItem("Select a user");
        //Add all the users from the current conference.
        for (int i = 0; i < my_data_store.getCurrentConference().getUsers().size(); i++)
        {
            my_users.addItem((String)my_data_store.getCurrentConference().getUsers().get(i).getName());
        }
        //Create a create User button
        create_user_btn = ImageCreator.createButton("    Create a User", new Rectangle(my_users.getBounds().x + my_users.getBounds().width + 10, my_users.getBounds().y - 5, 145, 40));
        create_user_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String user_name = JOptionPane.showInputDialog(null, "Please input new user name.", "Add a user");
                if (user_name.length() < 2)
                {
                    return;
                }
                //add the user to the conference in tthe data object
                my_data_store.getCurrentConference().addUser(new User(user_name));
                my_data_store.addConferenceObject(my_data_store.getCurrentConference());
                remove(my_users);
                my_users = new JComboBox();
                //Get the dimensions needed to say select a conference
                int hgt = metrics.getHeight();
                int adv = metrics.stringWidth("Select a user");
                new Dimension(adv+2, hgt+2);
                my_users.setBounds(my_conferences.getBounds().x, my_conferences.getBounds().y + my_conferences.getBounds().height + 50,300,30);
                my_users.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){

                        if (my_users.getSelectedItem().equals("Select a user"))
                        {
                            return;
                        }
                        my_users.setEnabled(false);
                        for (int i = 0; i < my_data_store.getCurrentConference().getUsers().size();i++)
                        {
                            if (my_data_store.getCurrentConference().getUsers().get(i).getName().equals((String)my_users.getSelectedItem()))
                            {
                                my_user = (User)my_data_store.getCurrentConference().getUsers().get(i);
                            }
                        }
                        create_user_btn.setEnabled(false);
                        displayUserRoleTypeDropDown();
                    }
                });
                add(my_users);
                my_users.addItem("Select a user");
                //Add all the users from the current conference.
                for (int i = 0; i < my_conference.getUsers().size(); i++)
                {
                    my_users.addItem(my_data_store.getCurrentConference().getUsers().get(i).getName());
                }
            }
        });
        add(create_user_btn);
        
        ///Here we set the action of the users drop down.
        my_users.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
                if (my_users.getSelectedItem().equals("Select a user"))
                {
                    return;
                }
                my_users.setEnabled(false);
                for (int i = 0; i < my_data_store.getCurrentConference().getUsers().size();i++)
                {
                    if (my_data_store.getCurrentConference().getUsers().get(i).getName().equals((String)my_users.getSelectedItem()))
                    {
                        my_user = (User)my_data_store.getCurrentConference().getUsers().get(i);
                    }
                }
                create_user_btn.setEnabled(false);
                displayUserRoleTypeDropDown();
                //displayGoButton();
            }
        });
        add(my_users);
        pack();
    }
    //the role are also depend on the user.
    private void displayUserRoleTypeDropDown()
    {
        JLabel selectRole = new JLabel("Select who you would like to\n enter this program as:");
        add(selectRole);
        int hgt = metrics.getHeight();
        int adv = metrics.stringWidth("Select a Role");
        Dimension size = new Dimension(adv+2, hgt+2);
        
        my_user_roles.setBounds(my_users.getBounds().x,my_users.getBounds().y + my_users.getBounds().height + 50,100 + size.width,30);
        selectRole.setBounds(my_user_roles.getBounds().x + 10,my_user_roles.getBounds().y - 15, 450, 15);
        my_user_roles.addItem("Select a role");
        //for all user check if the user name equal to the selected user name.

        //Add all the users from the current conference.
        for (int i = 0; i < my_user.getRoles().size(); i++)
        {
            my_user_roles.addItem(my_user.getRoles().get(i));
        }
        //Create a create User button
        assign_role_btn = ImageCreator.createButton("   Assign a Role", new Rectangle(my_user_roles.getBounds().x + my_user_roles.getBounds().width + 10, my_user_roles.getBounds().y, 145, 40));
        assign_role_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Object[] possibilities = {"Author", "PC", "Reviewer"};
                String role_type = (String)JOptionPane.showInputDialog(null, this,"Choose a role to assign to the user",JOptionPane.PLAIN_MESSAGE,null, possibilities,"Author");
                if (role_type.length() < 2)
                {
                    return;
                }
                remove(my_user_roles);
                //get selected role.
                if (role_type.equals("Author"))
                {
                    my_user.addRole(Role.AUTHOR);
                }
                else if (role_type.equals("PC"))
                {
                    my_user.addRole(Role.PC);
                }
                else if (role_type.equals("Reviewer"))
                {
                    my_user.addRole(Role.REVIEWER);
                }
                my_conference.removeUserByName(my_user.getName());
                my_conference.addUser(my_user);
                my_data_store.addConferenceObject(my_conference);
                my_user_roles = new JComboBox();
                //recreate the user roles drop down
                int hgt = metrics.getHeight();
                int adv = metrics.stringWidth("Select a Role");
                Dimension size = new Dimension(adv+2, hgt+2);
                my_user_roles.setBounds(my_users.getBounds().x,my_users.getBounds().y + my_users.getBounds().height + 50,100 + size.width,30);
                my_user_roles.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if (my_user_roles.getSelectedItem().equals("Select a role"))
                        {
                            return;
                        }
                        my_user_roles.setEnabled(false);
                        my_user.setCurrentRole((Role)my_user_roles.getSelectedItem());
                        //get selected role.
                        if (my_user_roles.getSelectedItem().equals("Author"))
                        {
                            my_user.setCurrentRole(Role.AUTHOR);
                        }
                        else if (my_user_roles.getSelectedItem().equals("PC"))
                        {
                            my_user.setCurrentRole(Role.PC);
                        }
                        else if (my_user_roles.getSelectedItem().equals("Reviewer"))
                        {
                            my_user.setCurrentRole(Role.REVIEWER);
                        }
                        assign_role_btn.setEnabled(false);
                        displayGoButton();
                    }
                });
                add(my_user_roles);
                my_user_roles.addItem("Select a role");
                //Add all the users from the current conference.
                for (int i = 0; i < my_user.getRoles().size(); i++)
                {
                    my_user_roles.addItem(my_user.getRoles().get(i));
                }
            }
        });
        add(assign_role_btn);
        
        //set the action of what happens after person selects the role
        my_user_roles.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if (my_user_roles.getSelectedItem().equals("Select a role"))
                {
                    return;
                }
                my_user_roles.setEnabled(false);
                my_user.setCurrentRole((Role)my_user_roles.getSelectedItem());
                //get selected role.
                if (my_user_roles.getSelectedItem().equals("Author"))
                {
                    my_user.setCurrentRole(Role.AUTHOR);
                }
                else if (my_user_roles.getSelectedItem().equals("PC"))
                {
                    my_user.setCurrentRole(Role.PC);
                }
                else if (my_user_roles.getSelectedItem().equals("Reviewer"))
                {
                    my_user.setCurrentRole(Role.REVIEWER);
                }
                assign_role_btn.setEnabled(false);
                //my_selected_role = (Role) my_user_roles.getSelectedItem();
                displayGoButton();
            }
        });
        add(my_user_roles);
        pack();
    }
    private void displayGoButton()
    {
        JButton go = ImageCreator.createButton("   Go", new Rectangle(300, 300, 75, 50));
        add(go);
        go.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
            	//CLose this frame and bring up the user story to perform.
                dispose();
                new ChooseUserStory(my_user.getCurrentRole(), my_conference, my_user, my_data_store);
            }
            
        });
        pack();
    }
    public static void main(String... args)
    {
        new ConferenceCreatorStart();
    }
}

